package com.feature.core.domain;

import java.io.Serializable;

import com.alibaba.fastjson2.annotation.JSONField;
import com.feature.core.constant.Constants;

/**
 * 响应信息主体
 *
 */
public class Response<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	private String code;

	private String msg;

	private T data;

	public static <T> Response<T> ok() {
		return restResult(null, Constants.SUCCESS, Constants.SUCCESS);
	}

	public static <T> Response<T> ok(T data) {
		return restResult(data, Constants.SUCCESS, Constants.SUCCESS);
	}

	public static <T> Response<T> ok(T data, String msg) {
		return restResult(data, Constants.SUCCESS, msg);
	}

	public static <T> Response<T> fail() {
		return restResult(null, Constants.FAIL, Constants.FAIL);
	}

	public static <T> Response<T> fail(String msg) {
		return restResult(null, Constants.FAIL, msg);
	}

	public static <T> Response<T> fail(String code, String msg) {
		return restResult(null, code, msg);
	}

	private static <T> Response<T> restResult(T data, String code, String msg) {
		Response<T> apiResult = new Response<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@JSONField(serialize = false)
	public boolean isSuccess() {
		return Constants.SUCCESS.equals(this.code);
	}
}
